using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Reflection;
using Microsoft.Office.Project.EPMSync.Config.Editor.Properties;

namespace Microsoft.Office.Project.EPMSync.Config.Editor
{
    public partial class FormSplash : Form
    {
        private const int TIMER_INTERVAL = 50;

        static FormSplash splash;
        static Thread spThread;
        static string status;

        double OpacityIncrement = 0.1;
        double OpacityDecrement = 0.2;

        public FormSplash()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
            this.Opacity = .00;
            timer1.Interval = TIMER_INTERVAL;
            timer1.Start();
            this.Size = this.BackgroundImage.Size;
          
        }

        static public void ShowSplashScreen()
        {
            if (splash != null)
                return;
            spThread = new Thread(new ThreadStart(FormSplash.ShowForm));
            spThread.IsBackground = true;
            spThread.SetApartmentState(ApartmentState.STA);
            spThread.Start();
        }

        //Property for returning instance
        public static FormSplash GetSplashForm
        {
            get
            {
                return splash;
            }
        }

        static private void ShowForm()
        {
            splash = new FormSplash();
            Application.Run(splash);
        }

        static public void CloseForm()
        {
            if (splash != null && splash.IsDisposed == false)
            {
                splash.OpacityIncrement = -splash.OpacityDecrement;
            }
            spThread = null;
            
            splash = null;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            lblStatus.Text = status;

            if (OpacityIncrement > 0)
            {
                if (this.Opacity < 1)
                    this.Opacity += OpacityIncrement;
            }
            else
            {
                if (this.Opacity > 0)
                    this.Opacity += OpacityIncrement;
                else
                {
                    this.Close();
                }
            }
        }

        public static void SetStatus(string newStatus)
        {
            status = newStatus;
            if (splash == null)
                return;
        }

        private void Splash_Load(object sender, EventArgs e)
        {
            lblUserName.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            lblVersion.Text = Resources.versionTag + this.ProductVersion;
        }

        public static string AssemblyVersion
        {
            get
            {
                Assembly assembly = typeof(Microsoft.Office.Project.EPMSync.Config.Editor.Program).Assembly;
                if (Attribute.GetCustomAttribute(assembly, typeof(AssemblyFileVersionAttribute)) != null)
                {
                    AssemblyFileVersionAttribute fileVersion =
                        (AssemblyFileVersionAttribute)Attribute.GetCustomAttribute(assembly,
                         typeof(AssemblyFileVersionAttribute));
                    return fileVersion.Version;
                }
                return null;
            }
        }

    }
}